package cn.com.kingkit.mvc.action.business;

import java.io.Serializable;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.kingkit.mvc.controller.BaseAction;
import cn.com.kingkit.mvc.controller.IAction;
import cn.com.kingkit.mvc.exception.BusinessException;
import cn.com.kingkit.mvc.model.business.Material;
import cn.com.kingkit.mvc.model.business.MaterialType;
import cn.com.kingkit.mvc.service.impl.business.MaterialService;
import cn.com.kingkit.mvc.service.impl.business.MaterialTypeService;
import cn.com.kingkit.mvc.util.PageController;

public class MaterialAction extends BaseAction implements IAction,Serializable{
	private final static Log logger = LogFactory.getLog(MaterialAction.class);
	private PageController pager = null;
	private MaterialService service = null;
	private Material material = null;
	private List<Material> mList = null; 
	private MaterialTypeService typeService = null;
	private List<MaterialType> typeList = null;
	
	public String execute(HttpServletRequest request,HttpServletResponse response, ServletConfig servletConfig) {
		String act = request.getParameter("act");
		if(null != act){
			if("list".equals(act)){
				//取列表
				if ("list".equals(act)) {
					int totalRows = service.getRecordCount(new Material());
					String URL = request.getRequestURI();
					this.pager.setURL(URL);
					this.pager.setTotalRowsAmount(totalRows);
					List<Material> resourceList = service.list(new Material(), this.pager.getPageStartRow(), pager.getPageSize(), null, true);
					typeList = typeService.listAll(new MaterialType());
					request.setAttribute("typeList", typeList);
					pager.setData(resourceList);
					return getReturnURL(LIST);
				}
			}
			//点了添加或者点了修改
			else if ("get".equals(act)) {	
				material = service.get(material);
				typeList = typeService.listAll(new MaterialType());
				request.setAttribute("typeList", typeList);
				return getReturnURL(EDIT);
			}
			//保存表单
			else if ("save".equals(act)) {
				try {
					service.save(material);
					return getReturnURL("getList");
				} catch (BusinessException e) {
					//保存原来表单已输入的内容
					request.setAttribute("material", material);
					addMessage(e.getMessage());
					return getReturnURL(EDIT);
				}
			}
			//删除
			else if ("del".equals(act)) {
				String[] materialIds = request.getParameterValues("materialId");
				for (String materialId : materialIds) {
					Material m = new Material();
					if(null != materialId && !("".equals(materialId))){
						m.setMaterial_id(materialId);
						service.delete(m);
					}
				}
				return getReturnURL("getList");
			}
		}
		return null;
	}

	public PageController getPager() {
		return pager;
	}

	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public MaterialService getService() {
		return service;
	}

	public void setService(MaterialService service) {
		this.service = service;
	}

	public Material getMaterial() {
		return material;
	}

	public void setMaterial(Material material) {
		this.material = material;
	}

	public List<Material> getMList() {
		return mList;
	}

	public void setMList(List<Material> list) {
		mList = list;
	}

	public List<MaterialType> getTypeList() {
		return typeList;
	}

	public void setTypeList(List<MaterialType> typeList) {
		this.typeList = typeList;
	}

	public MaterialTypeService getTypeService() {
		return typeService;
	}

	public void setTypeService(MaterialTypeService typeService) {
		this.typeService = typeService;
	}

}
